package com.tind.wyf.baselib.widget;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.HashMap;
import java.util.Map;

public class OffsetLinearLayoutManager extends LinearLayoutManager {
    private Map<Integer, Integer> heightMap = new HashMap<>();


    public OffsetLinearLayoutManager(Context context) {
        this(context, LinearLayoutManager.VERTICAL, false);
    }

    public OffsetLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    @Override
    public void onLayoutCompleted(RecyclerView.State state) {
        super.onLayoutCompleted(state);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            heightMap.put(i, view.getHeight());
        }
    }

    @Override
    public int computeVerticalScrollOffset(RecyclerView.State state) {
        if (getChildCount()==0)
            return 0;
        try {
            int firstVisiblePosition = findFirstCompletelyVisibleItemPosition();
            View firstVisiableView  = findViewByPosition(firstVisiblePosition);
            int offsetY  = -(int) (firstVisiableView.getY());
            for (int i = 0; i < firstVisiblePosition; i++) {
                offsetY += heightMap.get(i) == null ? 0 : heightMap.get(i);
            }
            return offsetY;
        } catch (Exception e) {
            return 0;
        }

    }
}
